<?php

class Update_stats
{
	public $swixs;
	
	function main()
	{
		SQL::update('forums', array(
			'topic_count' => 0,
			'post_count' => 0
		))->run();
		
		SQL::exec('SELECT parent AS forum, COUNT(*) AS sum
				   FROM '.SQL::$prefix.'topics
				   GROUP BY parent
				 ');
		
		$topics = SQL::result();
		
		foreach($topics as $topicstats)
		{
			SQL::update('forums', array(
				'topic_count' => $topicstats->sum
			))->where('id', $topicstats->forum)->run();
		}
		
		SQL::exec('SELECT t.parent AS forum, COUNT(p.id) AS sum 
				   FROM '.SQL::$prefix.'posts AS p 
				   LEFT JOIN '.SQL::$prefix.'topics AS t ON p.parent=t.id 
				   GROUP BY t.parent
				 ');
		foreach(SQL::result() as $key => $poststats)
		{
			SQL::update('forums', array(
				'post_count' => $poststats->sum
			))->where('id', $poststats->forum)->run();
		}
	}
}

?>